package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    //保存套餐和菜品的关系
    @Override
    public void saveWithSetmealDish(SetmealDto dto) {
        //1.保存套餐
        save(dto);
        //2.
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //给关联表创建套餐ID
        setmealDishes.forEach(item -> item.setSetmealId(dto.getId()));
        //保存套餐和菜品的关联关系
        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //传递过来的这些套餐中是否有起售的
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        int count = count(lqw);//起售的套餐数目
        if(count>0){
            //不删除
            throw new CustomException("有起售的套餐，不允许删除");
        }
        //删除套餐数据
        removeByIds(ids);
        //删除套餐关联菜品的数据
        LambdaQueryWrapper<SetmealDish> dishLqw = new LambdaQueryWrapper<>();
        //select * from b where id in (1,2,3,4,5)
        dishLqw.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(dishLqw);

    }
    //根据id查询回显数据
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDish = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(setmealDish);
        return setmealDto;
    }
//修改套餐（保存）
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
            //更新套餐表
        this.updateById(setmealDto);
        //清理当前套餐的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //添加新的套餐菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(item->item.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);
    }
}
